#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.gaffer.gaffer.catalogue import Catalogue
from oriolus.pith.packets.packet import FullName
from unittest import TestCase, TestLoader, TextTestRunner

class MockTrotter:
    pass

class TestCatalogue(TestCase):
    def test_identify_packets01(self):
        names = ("test-1.2.3.4", )
        get_packet_list = lambda: []
        expected = []

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets02(self):
        names = ("test_packet-1.2.3.4", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", ]
        expected = [FullName("test_packet", "1.2.3.4"), ]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets03(self):
        names = ("test_packet-1.2.3.4", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.5"]
        expected = [FullName("test_packet", "1.2.3.4"), ]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets04(self):
        names = ("test_packet-1.2.3.5", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.5"]
        expected = [FullName("test_packet", "1.2.3.5"), ]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets05(self):
        names = ("test_packet", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.5"]
        expected = [FullName("test_packet", "1.2.3.4"), FullName("test_packet", "1.2.3.5"),]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets06(self):
        names = ("test_packet", )
        get_packet_list = lambda: ["test_packet-1.2.3.5", "test_packet-1.2.3.4"]
        expected = [FullName("test_packet", "1.2.3.5"), FullName("test_packet", "1.2.3.4"),]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets07(self):
        names = ("test", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.5"]
        expected = [FullName("test_packet", "1.2.3.4"), FullName("test_packet", "1.2.3.5"),]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets08(self):
        names = ("test", )
        get_packet_list = lambda: ["test_packet-1.2.3.5", "test_packet-1.2.3.4"]
        expected = [FullName("test_packet", "1.2.3.5"), FullName("test_packet", "1.2.3.4"),]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets09(self):
        names = ("notest", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.5"]
        expected = []

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets10(self):
        names = ("test_packet-1.2.3.1", )
        get_packet_list = lambda: ["test_packet-1.2.3.1", "test_packet-1.2.3.10"]
        expected = [FullName("test_packet", "1.2.3.1")]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets11(self):
        names = ("test_packet-1.2.3.4", "test_packet2-2.3.4.5")
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.10"]
        expected = [FullName("test_packet", "1.2.3.4")]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets12(self):
        names = ("test_packet-1.2.3.4", "test_packet2-2.3.4.5")
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet2-2.3.4.5"]
        expected = [FullName("test_packet", "1.2.3.4"), FullName("test_packet2", "2.3.4.5")]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets13(self):
        names = ("packet", )
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet2-2.3.4.5"]
        expected = []

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets14(self):
        names = ("test_packet2-2.3.4.5", "test_packet-1.2.3.4")
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.10", "test_packet2-2.3.4.5"]
        expected = [FullName("test_packet2", "2.3.4.5"), FullName("test_packet", "1.2.3.4")]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def test_identify_packets15(self):
        names = ("test_packet2-2.3.4.5", "test_packet-1.2.3.4", "test")
        get_packet_list = lambda: ["test_packet-1.2.3.4", "test_packet-1.2.3.10", "test_packet2-2.3.4.5"]
        expected = [FullName("test_packet2", "2.3.4.5"), FullName("test_packet", "1.2.3.4"), FullName("test_packet", "1.2.3.10")]

        self.__assert_packets_were_identified(names, get_packet_list, expected)

    def __assert_packets_were_identified(self, names, trotter_get_packet_list, expected):
        trotter = MockTrotter()
        trotter.get_packet_list = trotter_get_packet_list

        catalogue = Catalogue(trotter)
        actual = catalogue.identify_packets(names)

        self.assertEqual(expected, actual)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestCatalogue)
    TextTestRunner(verbosity=2).run(suite)